{
 "cells": [
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "c52b24dc",
   "metadata": {},
   "source": [
    "# Pandas对象的索引\n",
    "\n",
    "可以对Pandas中的数据对象进行索引，得到数据的一部分。DataFrame和Series对象支持很多种索引方式："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "c91271f0",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "93e08d5f",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a7fd0c22",
   "metadata": {},
   "source": [
    "## 基于中括号的索引和切片\n",
    "\n",
    "Pandas数据对象可以使用一对中括号“[]”进行索引操作。"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "38150d66",
   "metadata": {},
   "source": [
    "在Pandas中，除了数字和字符串，时间序列也可以用来当做标记。日期时间序列可以用函数pd.date_range()创建："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "469427fa",
   "metadata": {},
   "outputs": [],
   "source": [
    "dates = pd.date_range('1/1/2000', periods=8)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "37542274",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "DatetimeIndex(['2000-01-01', '2000-01-02', '2000-01-03', '2000-01-04',\n",
       "               '2000-01-05', '2000-01-06', '2000-01-07', '2000-01-08'],\n",
       "              dtype='datetime64[ns]', freq='D')"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dates"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "d9437199",
   "metadata": {},
   "source": [
    "通过指定index参数，可以构建一个基于时间序列的DataFrame对象："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "3f550a0b",
   "metadata": {},
   "outputs": [],
   "source": [
    "df = pd.DataFrame(np.random.randn(8, 4), \n",
    "                  index=dates,\n",
    "                  columns=['A', 'B', 'C', 'D'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "b8bdc82c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2000-01-01</th>\n",
       "      <td>-0.681137</td>\n",
       "      <td>-0.160976</td>\n",
       "      <td>0.234236</td>\n",
       "      <td>-2.174047</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2000-01-02</th>\n",
       "      <td>0.231262</td>\n",
       "      <td>2.145968</td>\n",
       "      <td>-1.485805</td>\n",
       "      <td>1.616187</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2000-01-03</th>\n",
       "      <td>0.879341</td>\n",
       "      <td>-0.120279</td>\n",
       "      <td>-0.370795</td>\n",
       "      <td>0.738800</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2000-01-04</th>\n",
       "      <td>1.508886</td>\n",
       "      <td>0.266039</td>\n",
       "      <td>1.269122</td>\n",
       "      <td>-0.495479</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2000-01-05</th>\n",
       "      <td>-1.007768</td>\n",
       "      <td>-0.189645</td>\n",
       "      <td>-1.206941</td>\n",
       "      <td>1.050181</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2000-01-06</th>\n",
       "      <td>1.610657</td>\n",
       "      <td>-0.592687</td>\n",
       "      <td>-2.295021</td>\n",
       "      <td>0.503928</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2000-01-07</th>\n",
       "      <td>0.514324</td>\n",
       "      <td>1.002499</td>\n",
       "      <td>0.464693</td>\n",
       "      <td>1.156294</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2000-01-08</th>\n",
       "      <td>-0.972601</td>\n",
       "      <td>-0.465772</td>\n",
       "      <td>0.908502</td>\n",
       "      <td>0.071828</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                   A         B         C         D\n",
       "2000-01-01 -0.681137 -0.160976  0.234236 -2.174047\n",
       "2000-01-02  0.231262  2.145968 -1.485805  1.616187\n",
       "2000-01-03  0.879341 -0.120279 -0.370795  0.738800\n",
       "2000-01-04  1.508886  0.266039  1.269122 -0.495479\n",
       "2000-01-05 -1.007768 -0.189645 -1.206941  1.050181\n",
       "2000-01-06  1.610657 -0.592687 -2.295021  0.503928\n",
       "2000-01-07  0.514324  1.002499  0.464693  1.156294\n",
       "2000-01-08 -0.972601 -0.465772  0.908502  0.071828"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "3ddc1f59",
   "metadata": {},
   "source": [
    "对于Series对象来说，中括号索引标记返回一个标量；对于DataFrame对象来说，中括号索引列标记，返回该列对应的一个Series对象："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "3b4a1325",
   "metadata": {},
   "outputs": [],
   "source": [
    "s = df['A']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "8d5ec701",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2000-01-01   -0.681137\n",
       "2000-01-02    0.231262\n",
       "2000-01-03    0.879341\n",
       "2000-01-04    1.508886\n",
       "2000-01-05   -1.007768\n",
       "2000-01-06    1.610657\n",
       "2000-01-07    0.514324\n",
       "2000-01-08   -0.972601\n",
       "Freq: D, Name: A, dtype: float64"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "5cd5db21",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1.6106574534144948"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s[dates[5]]"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "bc1942bb",
   "metadata": {},
   "source": [
    "中括号索引还支持用列标记列表操作，得到一个DataFrame对象："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "559ba8db",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2000-01-01</th>\n",
       "      <td>-0.681137</td>\n",
       "      <td>-0.160976</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2000-01-02</th>\n",
       "      <td>0.231262</td>\n",
       "      <td>2.145968</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2000-01-03</th>\n",
       "      <td>0.879341</td>\n",
       "      <td>-0.120279</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2000-01-04</th>\n",
       "      <td>1.508886</td>\n",
       "      <td>0.266039</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2000-01-05</th>\n",
       "      <td>-1.007768</td>\n",
       "      <td>-0.189645</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2000-01-06</th>\n",
       "      <td>1.610657</td>\n",
       "      <td>-0.592687</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2000-01-07</th>\n",
       "      <td>0.514324</td>\n",
       "      <td>1.002499</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2000-01-08</th>\n",
       "      <td>-0.972601</td>\n",
       "      <td>-0.465772</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                   A         B\n",
       "2000-01-01 -0.681137 -0.160976\n",
       "2000-01-02  0.231262  2.145968\n",
       "2000-01-03  0.879341 -0.120279\n",
       "2000-01-04  1.508886  0.266039\n",
       "2000-01-05 -1.007768 -0.189645\n",
       "2000-01-06  1.610657 -0.592687\n",
       "2000-01-07  0.514324  1.002499\n",
       "2000-01-08 -0.972601 -0.465772"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df[['A', 'B']]"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "435694c3",
   "metadata": {},
   "source": [
    "当列标记的名称是一个合法的Python对象名时，在不跟DataFrame对象自带的方法和属性名冲突的情况下，Pandas会自动将该名称转化为一个属性："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "ec4c92d0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2000-01-01    0.234236\n",
       "2000-01-02   -1.485805\n",
       "2000-01-03   -0.370795\n",
       "2000-01-04    1.269122\n",
       "2000-01-05   -1.206941\n",
       "2000-01-06   -2.295021\n",
       "2000-01-07    0.464693\n",
       "2000-01-08    0.908502\n",
       "Freq: D, Name: C, dtype: float64"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.C"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "60d0a29a",
   "metadata": {},
   "source": [
    "Series对象也支持使用标记名称进行属性索引："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "0285ad8f",
   "metadata": {},
   "outputs": [],
   "source": [
    "sa = pd.Series([1,2,3],index=list('abc'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "762829ac",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sa.b"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "3a15386f",
   "metadata": {},
   "source": [
    "Series对象支持切片操作："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "ed53fece",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2000-01-01   -0.681137\n",
       "2000-01-02    0.231262\n",
       "2000-01-03    0.879341\n",
       "2000-01-04    1.508886\n",
       "2000-01-05   -1.007768\n",
       "Freq: D, Name: A, dtype: float64"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s[:5]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "21da467e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2000-01-01   -0.681137\n",
       "2000-01-03    0.879341\n",
       "2000-01-05   -1.007768\n",
       "2000-01-07    0.514324\n",
       "Freq: 2D, Name: A, dtype: float64"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s[::2]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d5d0d858",
   "metadata": {},
   "source": [
    "以及高级的切片操作："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "3e2bc90d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2000-01-01   -0.681137\n",
       "2000-01-02    0.231262\n",
       "2000-01-03    0.879341\n",
       "Freq: D, Name: A, dtype: float64"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s['20000101':'20000103']"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "ddb9fea5",
   "metadata": {},
   "source": [
    "## 基于位置和标记的高级索引\n",
    "\n",
    "在中括号索引时，DataFrame对象会存在列索引和行索引混用的情况：\n",
    "- 单值：索引列。\n",
    "- 列表：索引多列。\n",
    "- slice对象：索引多行。\n",
    "\n",
    "为了避免混淆，DataFrame对象提供了另一套高级索引来处理这些情况。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "15cb602b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2000-01-01</th>\n",
       "      <td>-0.681137</td>\n",
       "      <td>-0.160976</td>\n",
       "      <td>0.234236</td>\n",
       "      <td>-2.174047</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2000-01-02</th>\n",
       "      <td>0.231262</td>\n",
       "      <td>2.145968</td>\n",
       "      <td>-1.485805</td>\n",
       "      <td>1.616187</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2000-01-03</th>\n",
       "      <td>0.879341</td>\n",
       "      <td>-0.120279</td>\n",
       "      <td>-0.370795</td>\n",
       "      <td>0.738800</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2000-01-04</th>\n",
       "      <td>1.508886</td>\n",
       "      <td>0.266039</td>\n",
       "      <td>1.269122</td>\n",
       "      <td>-0.495479</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2000-01-05</th>\n",
       "      <td>-1.007768</td>\n",
       "      <td>-0.189645</td>\n",
       "      <td>-1.206941</td>\n",
       "      <td>1.050181</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2000-01-06</th>\n",
       "      <td>1.610657</td>\n",
       "      <td>-0.592687</td>\n",
       "      <td>-2.295021</td>\n",
       "      <td>0.503928</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2000-01-07</th>\n",
       "      <td>0.514324</td>\n",
       "      <td>1.002499</td>\n",
       "      <td>0.464693</td>\n",
       "      <td>1.156294</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2000-01-08</th>\n",
       "      <td>-0.972601</td>\n",
       "      <td>-0.465772</td>\n",
       "      <td>0.908502</td>\n",
       "      <td>0.071828</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                   A         B         C         D\n",
       "2000-01-01 -0.681137 -0.160976  0.234236 -2.174047\n",
       "2000-01-02  0.231262  2.145968 -1.485805  1.616187\n",
       "2000-01-03  0.879341 -0.120279 -0.370795  0.738800\n",
       "2000-01-04  1.508886  0.266039  1.269122 -0.495479\n",
       "2000-01-05 -1.007768 -0.189645 -1.206941  1.050181\n",
       "2000-01-06  1.610657 -0.592687 -2.295021  0.503928\n",
       "2000-01-07  0.514324  1.002499  0.464693  1.156294\n",
       "2000-01-08 -0.972601 -0.465772  0.908502  0.071828"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "ebaca17b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "A    0.231262\n",
       "B    2.145968\n",
       "C   -1.485805\n",
       "D    1.616187\n",
       "Name: 2000-01-02 00:00:00, dtype: float64"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.loc[dates[1]]"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "7e524a96",
   "metadata": {},
   "source": [
    "日期类型还使用一个字符串进行索引，只要这个字符串符合日期的格式，如："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "9d664267",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "A    0.231262\n",
       "B    2.145968\n",
       "C   -1.485805\n",
       "D    1.616187\n",
       "Name: 2000-01-02 00:00:00, dtype: float64"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.loc['20000102']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "2a3210b7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2000-01-02</th>\n",
       "      <td>0.231262</td>\n",
       "      <td>2.145968</td>\n",
       "      <td>-1.485805</td>\n",
       "      <td>1.616187</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2000-01-03</th>\n",
       "      <td>0.879341</td>\n",
       "      <td>-0.120279</td>\n",
       "      <td>-0.370795</td>\n",
       "      <td>0.738800</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2000-01-04</th>\n",
       "      <td>1.508886</td>\n",
       "      <td>0.266039</td>\n",
       "      <td>1.269122</td>\n",
       "      <td>-0.495479</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                   A         B         C         D\n",
       "2000-01-02  0.231262  2.145968 -1.485805  1.616187\n",
       "2000-01-03  0.879341 -0.120279 -0.370795  0.738800\n",
       "2000-01-04  1.508886  0.266039  1.269122 -0.495479"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.loc['20000102':'20000104']"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "66ea3570",
   "metadata": {},
   "source": [
    "与数字切片只包含开头不包含结尾的结果不同，`.loc`属性的行切片结果包含结尾。"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "09fff4a3",
   "metadata": {},
   "source": [
    "`.loc`属性还可以额外接受列标记进行索引。例如："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "0e1445fc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "-0.6811372986010428"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.loc['20000101', 'A']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "85e3d61e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "A   -0.681137\n",
       "C    0.234236\n",
       "Name: 2000-01-01 00:00:00, dtype: float64"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.loc['20000101', ['A', 'C']]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "4dfbeec7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>C</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2000-01-07</th>\n",
       "      <td>0.514324</td>\n",
       "      <td>0.464693</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2000-01-08</th>\n",
       "      <td>-0.972601</td>\n",
       "      <td>0.908502</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                   A         C\n",
       "2000-01-07  0.514324  0.464693\n",
       "2000-01-08 -0.972601  0.908502"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.loc['20000107':, ['A', 'C']]"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "45beab71",
   "metadata": {},
   "source": [
    "`.loc`属性基于标记对DataFrame对象进行索引，而`.iloc`属性则基于位置对DataFrame对象进行索引。例如，用`.iloc`属性索引第二行的数据："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "aaba503e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "A    0.231262\n",
       "B    2.145968\n",
       "C   -1.485805\n",
       "D    1.616187\n",
       "Name: 2000-01-02 00:00:00, dtype: float64"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.iloc[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "57dad418",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.23126158547402592"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.iloc[1, 0]"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "c4d44bb6",
   "metadata": {},
   "source": [
    "与.loc属性不同的是，.iloc的切片不包含最后一个元素。"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "3d16c214",
   "metadata": {},
   "source": [
    "如果只需要索引单个值，最快速的方法是使用`.at`索引标记："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "11391c06",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1.6106574534144948"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.at[dates[5], 'A']"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "bbe1ab14",
   "metadata": {},
   "source": [
    "或者用`.iat`索引位置："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "7ddeb6f9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1.6106574534144948"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.iat[5, 0]"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "7dc492f5",
   "metadata": {},
   "source": [
    "Pandas支持基于布尔值的条件对Series进行进行索引。例如，使用布尔值条件s>0索引，来得到Series对象中所有大于0的值："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "40aff2b2",
   "metadata": {},
   "outputs": [],
   "source": [
    "s = df[\"A\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "98474ab3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2000-01-02    0.231262\n",
       "2000-01-03    0.879341\n",
       "2000-01-04    1.508886\n",
       "2000-01-06    1.610657\n",
       "2000-01-07    0.514324\n",
       "Name: A, dtype: float64"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s[s > 0]"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "61103f8e",
   "metadata": {},
   "source": [
    "还可以使用取反符号`~`，使用取反表达式`~(s>0)`进行索引，得到所有小于等于0的值："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "51b78999",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2000-01-01   -0.681137\n",
       "2000-01-05   -1.007768\n",
       "2000-01-08   -0.972601\n",
       "Name: A, dtype: float64"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s[~(s > 0)]"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "973502a7",
   "metadata": {},
   "source": [
    "DataFrame对象的行也可以通过布尔值条件索引。比如，索引所有A列大于0的行："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "cf6465fc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2000-01-02</th>\n",
       "      <td>0.231262</td>\n",
       "      <td>2.145968</td>\n",
       "      <td>-1.485805</td>\n",
       "      <td>1.616187</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2000-01-03</th>\n",
       "      <td>0.879341</td>\n",
       "      <td>-0.120279</td>\n",
       "      <td>-0.370795</td>\n",
       "      <td>0.738800</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2000-01-04</th>\n",
       "      <td>1.508886</td>\n",
       "      <td>0.266039</td>\n",
       "      <td>1.269122</td>\n",
       "      <td>-0.495479</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2000-01-06</th>\n",
       "      <td>1.610657</td>\n",
       "      <td>-0.592687</td>\n",
       "      <td>-2.295021</td>\n",
       "      <td>0.503928</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2000-01-07</th>\n",
       "      <td>0.514324</td>\n",
       "      <td>1.002499</td>\n",
       "      <td>0.464693</td>\n",
       "      <td>1.156294</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                   A         B         C         D\n",
       "2000-01-02  0.231262  2.145968 -1.485805  1.616187\n",
       "2000-01-03  0.879341 -0.120279 -0.370795  0.738800\n",
       "2000-01-04  1.508886  0.266039  1.269122 -0.495479\n",
       "2000-01-06  1.610657 -0.592687 -2.295021  0.503928\n",
       "2000-01-07  0.514324  1.002499  0.464693  1.156294"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df[df[\"A\"] > 0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "80e302a4",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
